package com.javen.bankpay;

import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class AgentService {
	
	public List<Record> findAgentTrans(String mch_id, String mch_name){
		String sql = "select * from merch_info a where a.merchno = '"+mch_id+"' and a.merch_name = '"+mch_name+"' ";
		return Db.use("db2").find(sql);
	}
	
	public List<Record> findAgents(String openID, String mch_id){
		String sql = "select * from user_agent a where a.openID = '"+openID+"' ";
		if(mch_id != null && mch_id.length() > 0){
			sql += " and a.mch_id = '"+mch_id+"' ";
		}
		return Db.find(sql);
	}
	
	public void save(Record record){
		Db.save("user_agent", record);
	}
	
	public int getOrderCount(String number, String dateFrom, String dateTo, String type){
		String sql = "select count(*) as count from qrcode_orderinfo a where a.merchNo = '"+number+"' and a.orderDate > '"+dateFrom
				+"' and a.orderDate < '"+dateTo+"' ";
		if(!type.equals("all")){
			sql += " and a.orderType = '"+type+"' ";
		}
		return Db.use("db2").findFirst(sql).getLong("count").intValue();
	}
	
	public List<Record> getOrders(String number, String dateFrom, String dateTo, String type, int start){
		String sql = "select a.orderDate, ifnull(price,0.00) as price,ifnull(realPrice,0.00) as realPrice,ifnull(feePrice,0.00) as feePrice "
				+ " from qrcode_orderinfo a where a.merchNo = '"+number+"' and a.orderDate > '"+dateFrom
				+"' and a.orderDate < '"+dateTo+"' ";
		if(!type.equals("all")){
			sql += " and a.orderType = '"+type+"' ";
		}
		sql += " order by oid limit "+start+", 5";
		return Db.use("db2").find(sql);
	}
	
}
